package Linux五种IO模型.reactor2;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author coderFu
 * @create 2022/3/8
 */
public class WriteState implements HandlerState {


    @Override
    public void changeState(HandlerDemo2 h) {

    }

    @Override
    public void handle(HandlerDemo2 h, SelectionKey sk, SocketChannel sc, ThreadPoolExecutor pool) throws IOException {

        String str = "发送信息给客户端";

        ByteBuffer buf = ByteBuffer.wrap(str.getBytes());

        while (buf.hasRemaining()){
            sc.write(buf);
        }

        h.setState(new ReadState());
        sk.interestOps(SelectionKey.OP_READ);
        sk.selector().wakeup(); // 是一个阻塞的selector 操作立刻返回


    }

    class WriteThread implements Runnable{

        private HandlerDemo2 h;

        private String response;

        public  WriteThread(HandlerDemo2 h, String response){
            this.h = h;
            this.response = response;
        }

        @Override
        public void run() {
            // process();
        }
    }
}
